package com.calefk.util {
	
    import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
    import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLLoaderDataFormat;
	
	public class TextLoader extends FileLoader {
        
        public static const TYPE_XML:int = 0;
		
		private var mUrlLoader:URLLoader;
		private var mUrlRequest:URLRequest;
        private var mType:int;
		
		public function TextLoader(filePath: String , type:int = 0) {
			isLoad = false;
            mType = type;
			mUrlLoader = new URLLoader();
			mUrlRequest = new URLRequest(filePath);
			// mUrlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			mUrlLoader.addEventListener(Event.COMPLETE, completeFile , false, 0 , true);
			mUrlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorFile , false, 0 , true);
            mUrlLoader.addEventListener(ProgressEvent.PROGRESS, progressFile , false , 0 , true);
            mUrlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorFile , false , 0 , true);
			mUrlLoader.load(mUrlRequest);
		}
		  
		override protected function completeFile(event:Event):void {
            
			try {
                switch (mType) {
                    case TYPE_XML:
                        document = new XML(event.target.data);
                        break;
                }
                isLoad = true;

            } catch (e:TypeError) {
                isLoad = false;
                trace(":: ERROR -> File Loader : " + e.getStackTrace());
            }
            
            cleanEvents();			
            
			if (onLoadComplete != null) {
				onLoadComplete(this); 
			}
		}
        
        override protected function progressFile(event:ProgressEvent):void {
            mBytesLoaded = event.bytesLoaded;
            mBytesTotal = event.bytesTotal;
            
            if (onProgress != null) {
                onProgress(mBytesLoaded , mBytesTotal);
            }
        }
        
        private function ioErrorFile(event:IOErrorEvent):void {
            trace(":: ERROR -> ioErrorFile : " + event.text);
        }
        
        private function securityErrorFile(event:SecurityError):void {
            trace(":: ERROR -> securityErrorFile : " + event.getStackTrace);            
        }
        
        private function cleanEvents():void {
  			mUrlLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorFile);
            mUrlLoader.removeEventListener(Event.COMPLETE , completeFile);
            mUrlLoader.removeEventListener(ProgressEvent.PROGRESS , progressFile);
            mUrlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorFile);
        }
		
		override public function clean():void {
            cleanEvents();
            
			mUrlLoader = null;
			mUrlRequest = null;
			document = null;
            
			if (onLoadComplete != null) {
                onLoadComplete = null;
            }
		}
	}
}
